package com.runtime.hongyunmanage.controller;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.runtime.hongyunmanage.domain.Message;
import com.runtime.hongyunmanage.domain.vo.OrdersVo;
import com.runtime.hongyunmanage.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @GetMapping("/select")
    public JSONObject selectOrder( int current, int size) {
        String status=null;
        String createdAt=null;
        String username=null;
        String endAt=null;
        int currentpage = (current - 1) * size;//从当前页第几条
        List<OrdersVo> getmanager = ordersService.selectOrder(currentpage, size);
        int select= ordersService.selectone(createdAt,endAt,username,status);
        JSONObject json = new JSONObject();
        json.put("list", getmanager);
        json.put("total", select);
        return json;
    }

    //搜索框查询
    @PostMapping("/search")
    public JSONObject getuser(
                            @RequestParam(value = "createdAt", required = false) String createdAt,
                            @RequestParam(value = "endAt", required = false) String endAt,
                            @RequestParam(value = "username", required = false) String username,
                            @RequestParam(value = "status", required = false) String status,
                            @RequestParam(value = "current", required = false)  int current,
                            @RequestParam(value = "size", required = false) int size){
        int currentpage = (current - 1) * size;//从当前页第几条
        List<OrdersVo> get = ordersService.selectroleuserAcc(createdAt,endAt,username,status,currentpage, size);
        int select = ordersService.selectone(createdAt,endAt,username,status);
        JSONObject json = new JSONObject();
        json.put("list", get);
        json.put("total", select);
        return json;
    }




    //未读消息查询
    @PostMapping("/unread")
    public Integer unread(){
        Integer count=ordersService.MessageCount();
        return  count;
    }

    //消息查询
    @PostMapping("/selectMessage")
    public JSONObject selectMessage(int current, int size){
        int currentpage = (current - 1) * size;//从当前页第几条
        List<Message> get = ordersService.selectMessage(currentpage, size);
        int select = ordersService.totalMessage();
        JSONObject json = new JSONObject();
        json.put("list", get);
        json.put("total", select);
        return json;
    }

    //按钮修改状态
    @PutMapping("/updateStatue")
    public Integer updateStatue(@RequestParam Integer messageId) {
        return ordersService.updateStatue(messageId);
    }


    @DeleteMapping
    public Integer deleteMessage(@RequestParam Integer messageId) {
        return ordersService.deleteMessage(messageId);
    }

    @PutMapping("/markAll")
    public Integer markAll(@RequestParam Integer param) {
        return ordersService.markAll(param);
    }

    @PostMapping("/confirmShipment")
    public Result confirmShipment(@RequestParam(value = "orderId", required = false) Integer orderId,
                                  @RequestParam(value = "trackingNumber", required = false) String trackingNumber,
                                  @RequestParam(value = "courierCompany", required = false) String courierCompany) {
        // 调用服务层方法确认发货
        System.out.println(orderId);
        System.out.println(trackingNumber);
        System.out.println(courierCompany);
        boolean success = ordersService.confirmShipment(orderId, trackingNumber, courierCompany);
        if (success) {
            return new Result(Code.UPDATE_OK, "确认发货成功");
        } else {
            return new Result(Code.UPDATE_ERR, "确认发货失败");
        }
    }
}
